#include "msp430x41x2.h"
#include "user.h"
//ref voltage = 1.5v
//1.5v = 0x03ff
//unsigned char channel_changed=0;
unsigned char old_chan=0;


void Initial_ADC(void)
{
  ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON;
//  ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
//  ADC10AE0 |= 0x01;                         // P7.4 ADC option select
}

void Close_ADC(void)
{
  ADC10CTL0 = 0;
}


unsigned int Sample_ADC(unsigned char channel)
{
unsigned int tmp16=0;
  if(old_chan!=channel)
  {
    Close_ADC();
    Initial_ADC();     
  }
  
  if(channel==0)
  {
    old_chan=0;
    ADC10CTL1 = INCH_0;
    ADC10AE0 |= 0x01;                         // P7.4 ADC option select

  }
  if(channel==11)
  {
    old_chan=11; 
    ADC10CTL1 = INCH_11;
    ADC10AE0 = 0x00;                         // disable  
  }
  
    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
//    __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit
      while (!(ADC10IFG & ADC10CTL0));       // ADC10 Conversion Complete?
    tmp16=ADC10MEM;
    return tmp16;
}


// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
  __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)
}


unsigned char Get_BATT_Voltage(void)
{
  unsigned int tmp16=0;
//  Close_ADC();
//  Initial_ADC();  
  tmp16=Sample_ADC(11);
  if(tmp16>930)//3v
    return HIGH;
  if((tmp16>860)&&(tmp16<=930))//2.8v
    return NORMAL;
  if(tmp16<=860)
    return LOW;
  
  return NORMAL;
}

unsigned int Get_Input(void)
{
unsigned int tmp16=0;
  tmp16=Sample_ADC(0);
  return tmp16;
}






